<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\PayRequest;
use App\Models\Contract;
use App\Models\Houses;
use App\Models\Pay;
use App\Models\Project;
use App\Models\Survey;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class PayController extends Controller
{
    // 支付列表
    public function index(Request $request)
    {
        $projects = Project::all();
        $blocks = Survey::select('id', 'block_name')->get();
        $houses = Houses::all();


        $where = function ($query) use ($request) {
            if ($request->has('is_pay') and $request->is_pay != '') {
                $query->whereIsAudit($request->is_pay);
            }

            if ($request->has('project_id') and $request->project_id != '') {
                $query->whereProjectId($request->project_id);
            }

            if ($request->has('block_id') and $request->block_id != '') {
                $query->whereBlockId($request->block_id);
            }


        };


        $where_house = function ($query) use ($request) {
            if ($request->has('levy_house_num') and $request->levy_house_num != '') {
                $query->where('levy_house_num' ,$request->levy_house_num);
            }


            if ($request->has('owner') and $request->owner != '') {
                $query->where('owner', $request->owner);
            }
        };

        $pays = Contract::with(['projects' => function ($query) {
            $query->where('status', 3);

        }])->with(['houses' => function($query) use($where_house){

            $query->where($where_house);

        }])->where($where)->where('is_send_pay', 1)->paginate(10);


        return view('admin.pay.index', compact('projects', 'blocks', 'houses', 'pays'));
    }

    // 是否支付
    public function change_on(Request $request)
    {
        $id = $request->id;

        $contract = Contract::find($id);

        $value = $contract->is_pay ? false : true;
        $contract->is_pay = $value;

        $contract->save();

        if ($value == 1) {
            return ['msg' => '支付成功~~', 'status' => 1];
        } else {
            return ['msg' => '支付失败~~', 'status' => 1];
        }

    }

    // 编辑支付信息
    public function editPay(Request $request)
    {
        $arr=explode('=',$request->payId);
        $payId=$arr['1'];
        $payInfo= Contract::with('houses')->with('pays')->find($payId);


        return view('admin.pay.editPayInfo',compact('payInfo'));
    }

    public function updatePay(PayRequest $request)
    {


        $payInfo = Pay::with('contracts')->where('contract_id', $request->contract_id)->first();

        $data = [
            'contract_id' => $request->contract_id,
            'collect_account' => $request->collect_account,
            'collect_bank' => $request->collect_bank,
            'pay_account' => $request->pay_account,
            'pay_bank' => $request->pay_bank,
        ];

        if($payInfo){
            Pay::where('contract_id', $request->contract_id)->update($data);
            return  back()->with('status','修改支付信息成功！');
        }else{
            Pay::create($data);
            return  back()->with('status','填写支付信息成功！');
        }






    }
}
